//
//  STSubjectView.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STSubjectView.h"
#import "STSubjectBean.h"
@interface STSubjectView()<UIScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView *scrollView;
@end

#define BTN_SIZE 120 * ST_RATE

#define vertical_margin 30 * ST_RATE

#define horizontal_margin 40 * ST_RATE

@implementation STSubjectView

- (void)commitInit {
    [super commitInit];
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    
    self.scrollView = scrollView;
    
    scrollView.showsVerticalScrollIndicator = false;
    
    scrollView.showsHorizontalScrollIndicator = false;
    
    scrollView.delegate = self;
    
    scrollView.bounces = false;
    
    [self addSubview:scrollView];
    
}


- (void)updateData:(id)data {
    if (self.dataArray.count) {
        [self.dataArray removeAllObjects];
        
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
            obj = nil;
        }];
    }
    [self.dataArray addObjectsFromArray:data];
    
    for (int i = 0; i < self.dataArray.count; i++) {
        
        STSubjectBean *subject = self.dataArray[i];
        
        STButton *btn = [STButton getInstance];
        
        btn.urlString = subject.imgUrl;
        
        [btn setTitle:subject.name forState:UIControlStateNormal];
        
        [btn setTitleColor:ST_666666_COLOR forState:UIControlStateNormal];
        
        btn.titleLabel.font = ST_11_FONT;
        
        btn.tag = i + 100;
        
        [self.scrollView addSubview:btn];
        
        [btn addTarget:self action:@selector(subjectTriggered:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    NSInteger maxCol = 4;
    
    CGFloat mid_space = (ST_SCREEN_WIDTH - horizontal_margin * 2 - maxCol * BTN_SIZE) / (maxCol - 1);
    
    CGFloat height = (self.k_height - vertical_margin * 3 ) / 2;
    
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSUInteger col = idx % maxCol;
        
        NSUInteger row = idx / maxCol;
        
        obj.frame = CGRectMake(horizontal_margin + (BTN_SIZE + mid_space) * col  , vertical_margin + (height + vertical_margin )* row , BTN_SIZE, height);
        
    }];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    
    NSInteger maxCol = 4;
    
    CGFloat mid_space = (ST_SCREEN_WIDTH - horizontal_margin * 2 - maxCol * BTN_SIZE) / (maxCol - 1);
    
    CGFloat height = (self.k_height - vertical_margin * 3 ) / 2;
    
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSUInteger col = idx % maxCol;
        
        NSUInteger row = idx / maxCol;
        
        obj.frame = CGRectMake(horizontal_margin + (BTN_SIZE + mid_space) * col  , vertical_margin + (height + vertical_margin ) * row , BTN_SIZE, height);
        
    }];
    
    self.scrollView.contentSize = CGSizeMake(ST_SCREEN_WIDTH, self.k_height);
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
        [self.mDelegate scrollViewWillBeginDragging:self.scrollView];
    }
}
- (void)subjectTriggered:(UIButton *)sender {
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        
        [self.mDelegate onItemClick:STItemClickTypeSubject withData:self.dataArray[sender.tag - 100] forIndexPath:nil];
        
    }
}
@end
